//
//  SeasonViewController.m
//  美食百科
//
//  Created by dllo on 16/1/28.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "SeasonViewController.h"
#import "NewWorking.h"
#import "ClassModel.h"
#import "UIImageView+WebCache.h"
#import "SeasonTableViewCell.h"
#import "StapleViewController.h"
#import "Reachability.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height

@interface SeasonViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *seasonView;
@property(nonatomic,retain)NSMutableArray *seasonArray;
@end

@implementation SeasonViewController
- (void)dealloc
{   [_leftString release];
    [_urlString release];
    [_seasonView release];
    [_seasonArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    
    UIBarButtonItem *leftItemFirst = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-zuojiantou-3"] style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
    
    UIBarButtonItem *leftItemSecond = [[UIBarButtonItem alloc]initWithTitle:self.leftString style:UIBarButtonItemStyleDone target:self action:nil];
    self.navigationItem.leftBarButtonItems = @[leftItemFirst,leftItemSecond];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        
        //如果无网络,弹窗提示
        if ( 0 == reach.currentReachabilityStatus){
            //弹窗提示
            UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
            nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
            nonconnectionLabel.backgroundColor = [UIColor redColor];
            nonconnectionLabel.text = @"网络不给力,刷新试试~";
            nonconnectionLabel.textColor = [UIColor whiteColor];
            [self.view addSubview:nonconnectionLabel];
            
            //动画效果,通知栏缓慢出现
            [UIView animateWithDuration:0.6 animations:^{
                nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
            } completion:^(BOOL finished) {
                [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                    nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                    [nonconnectionLabel release];
                    
                } completion:^(BOOL finished) {
                       
                }];
                
                //            [UIView animateWithDuration:1.9 animations:^{
                //                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                //            //[nonconnectionLabel removeFromSuperview];
                //                [nonconnectionLabel release];
                //            }];
                
            }];
            
        }
    });

    
        self.seasonArray = [NSMutableArray array];
dispatch_async(dispatch_get_main_queue(), ^{
    [NewWorking GetDayaWithURL:[NSString stringWithFormat:@"http://food.boohee.com/fb/v1/categories?kind=%@",self.urlString] dic:nil sussess:^(id responseObject) {
        
        for (NSDictionary *dic in responseObject[@"categories"]) {
            ClassModel *class = [[ClassModel alloc]init];
            
//            [class setValue:dic[@"id"] forKey:@"Id"];
//            [class setValue:dic[@"name"] forKey:@"name"];
//            [class setValue:dic[@"image_url"] forKey:@"image_url"];
//            [class setValue:dic[@"description"] forKey:@"description"];

            [class setValuesForKeysWithDictionary:dic];
            
            [self.seasonArray addObject:class];
            [class release];
        }
        [self.seasonView reloadData];
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];

});
        self.seasonView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, (kHeight-64)/2) style:UITableViewStylePlain];
    self.seasonView.bounces = NO;
    self.seasonView.dataSource = self;
    self.seasonView.backgroundColor = [UIColor clearColor];

    self.seasonView.delegate = self;
    
    [self.view addSubview:self.seasonView];
    [_seasonView release];
[self.seasonView registerClass:[SeasonTableViewCell class] forCellReuseIdentifier:@"season"];

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.seasonArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SeasonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"season"];
    
    cell.seasons = self.seasonArray[indexPath.row];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

          return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return (kHeight-64)/2/self.seasonArray.count;
}


//跳转列表界面
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    StapleViewController *staple = [[StapleViewController alloc]init];
    staple.kindStr = self.urlString;
    staple.classModel = self.seasonArray[indexPath.row];
    [self.navigationController pushViewController:staple animated:YES];
    
    [staple release];
}


-(void)backAction:(UIBarButtonItem *)item{
    [self.navigationController popViewControllerAnimated:YES];
    self.navigationController.navigationBarHidden = YES;
}


- (BOOL)prefersStatusBarHidden{
    NSLog(@"ne");
    return NO;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
